package com.zhagnfei;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Arrays;

public class MonoFluxDemo {
    public static void main(String[] args) {
//        testMonoJust();
//        testFluxFromIterable();
        testMonoEmpty();
//        testMonoDefer();
//        testMonoError();
//        testMonoZip();
//        testFluxConcat();
//        testFluxMerge();
//        testFluxFlatMap();
//        testFluxFilter();
//        testFluxMap();
    }

    // Mono.just 创建一个包含单个元素的Mono
    private static void testMonoJust() {
        Mono<String> mono = Mono.just("Hello");
        mono.subscribe(System.out::println); // 输出: Hello
    }

    // Flux.fromIterable 将Iterable转换为Flux
    private static void testFluxFromIterable() {
        Flux<String> flux = Flux.fromIterable(Arrays.asList("A", "B", "C"));
        flux.subscribe(System.out::println); // 输出: A B C
    }


    // Mono.empty 测试
    private static void testMonoEmpty() {
        Mono<Void> emptyMono = Mono.empty();
        emptyMono.subscribe(
                data -> System.out.println("This won't be printed"),
                error -> System.err.println("Error, if any"),
                () -> System.out.println("Mono completed without value") // 输出: Mono completed without value
        );
    }

    // Mono.defer 测试
    private static void testMonoDefer() {
        Mono<Integer> deferredMono = Mono.defer(() -> Mono.just(42));
        deferredMono.subscribe(System.out::println); // 每次订阅都会计算新值，输出: 42
    }

    // Mono.error 测试
    private static void testMonoError() {
        Mono<String> errorMono = Mono.error(new IllegalStateException("An error occurred"));
        errorMono.subscribe(
                data -> System.out.println("This won't be printed"),
                error -> System.err.println(error.getMessage()) // 输出: An error occurred
        );
    }

    // Mono.zip 测试
    private static void testMonoZip() {
        Mono<String> mono1 = Mono.just("Hello");
        Mono<String> mono2 = Mono.just("World");
        Mono.zip(mono1, mono2, (s1, s2) -> s1 + " " + s2)
                .subscribe(System.out::println); // 输出: Hello World
    }

    // Flux.concat 测试
    private static void testFluxConcat() {
        Flux<String> flux1 = Flux.just("A", "B");
        Flux<String> flux2 = Flux.just("C", "D");
        Flux.concat(flux1, flux2).subscribe(System.out::println); // 输出: A B C D
    }

    // Flux.merge 测试
    private static void testFluxMerge() {
        Flux<String> flux1 = Flux.just("1", "3").delayElements(Duration.ofMillis(100));
        Flux<String> flux2 = Flux.just("2", "4");
        Flux.merge(flux1, flux2).subscribe(System.out::println); // 输出可能交错，如: 1 2 3 4
    }

    // Flux.flatMap 测试
    private static void testFluxFlatMap() {
        Flux<Integer> numbers = Flux.range(1, 3);
        Flux<String> mapped = numbers.flatMap(i -> Flux.just("Number: ", String.valueOf(i)));
        mapped.subscribe(System.out::println); // 输出: Number: 1 Number: 2 Number: 3
    }

    // Flux.filter 测试
    private static void testFluxFilter() {
        Flux<Integer> numbers = Flux.range(1, 5);
        Flux<Integer> filtered = numbers.filter(i -> i % 2 == 0);
        filtered.subscribe(System.out::println); // 输出: 2 4
    }

    // Flux.map 测试
    private static void testFluxMap() {
        Flux<String> words = Flux.fromIterable(Arrays.asList("apple", "banana", "cherry"));
        Flux<String> upperCased = words.map(String::toUpperCase);
        upperCased.subscribe(System.out::println); // 输出: APPLE BANANA CHERRY
    }
}
